<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends MY_Controller {

	public function __construct(){
		parent::__construct();
		/*构造方法中加载模型和辅助函数*/
	    $this->load->model(array('Article_model','Categoryes_model'));
		$this->load->helper('text');
	}
	/*读取文章列表*/
	public function index(){   
		/*加载分页类*/
	    $this->load->library('pagination');
		/*配置分页类*/
	    $config = array(
				'base_url'       => site_url().'/'.$this->uri->segment(1).'/'.$this->uri->segment(2),
				'total_rows'     => $this->db->count_all('articles'),
				'per_page'       => 14,
				'num_links'      => 5,
				'first_link'     => FALSE,
				'last_link'      => FALSE,
				'full_tag_open'  => "<ul class='pagination'>",                  //关闭标签
				'full_tag_close' => '</ul>',
				'num_tag_open'   => '<li>',                                  	//数字html
				'num_tag_close'  => '</li>',	                                //当前页html
				'cur_tag_open'   => "<li class='active'><a href='javascript:void(0),'>",
				'cur_tag_close'  => "</a></li>",
				'next_tag_open'  => '<li>',	                                    //上一页下一页html
				'next_tag_close' => '</li>',
				'prev_tag_open'  => '<li>',
				'prev_tag_close' => '</li>',
				'prev_link'      => "<i class='iconfont'>&#xe63e;</i>",
				'next_link'      => "<i class='iconfont'>&#xe63b;</i>"
	   );
	    /*传递分页配置到分页类的方法中*/
	    $this->pagination->initialize($config);
		/*从数据库中取出数据放入数组中*/
	    $data=array(
				  /*获取文章*/
				 'article'       => $this->Article_model->get_article($id=FALSE,$config['per_page'],$this->uri->segment(3)),
				  /*计算总数*/
				 'article_nums'  => $this->db->count_all('articles'),
				  /*获取栏目*/
				 'categoryes'    =>$this->Categoryes_model->get_categoryes()
	          ); 
        /*将结果查询结果分配至视图*/
	    $this->load->view('article_list',$data);

	}
	
	/*搜索文章列表*/
	public function search(){   
		/*加载分页类*/
	    $this->load->library('pagination');
		/*配置分页类*/
	    $config = array(
				'base_url'       => site_url().'/'.$this->uri->segment(1).'/'.$this->uri->segment(2),
				'total_rows'     => $this->Article_model->search_article_nums($this->input->post('cid'),$this->input->post('title')),
				'per_page'       => 14,
				'num_links'      => 5,
				'first_link'     => FALSE,
				'last_link'      => FALSE,
				'full_tag_open'  => "<ul class='pagination'>",                           //关闭标签
				'full_tag_close' => '</ul>',
				'num_tag_open'   => '<li>',	                                             //数字html
				'num_tag_close'  => '</li>',	                                         //当前页html
				'cur_tag_open'   => "<li class='active'><a href='javascript:void(0),'>",
				'cur_tag_close'  => "</a></li>",
				'next_tag_open'  => '<li>',	                                             //上一页下一页html
				'next_tag_close' => '</li>',
				'prev_tag_open'  => '<li>',
				'prev_tag_close' => '</li>',
				'prev_link'      => "<i class='iconfont'>&#xe63e;</i>",
				'next_link'      => "<i class='iconfont'>&#xe63b;</i>"
	   );
	    /*传递分页配置到分页类的方法中*/
	    $this->pagination->initialize($config);
		/*从数据库查询数据放入数组中*/
	    $data=array(
		         /*搜索文章*/
				 'article'       => $this->Article_model->search_article($this->input->post('cid'),$this->input->post('title'),$config['per_page'],$this->uri->segment(3)),
				 /*统计条数*/
				 'article_nums'  => $this->Article_model->search_article_nums($this->input->post('cid'),$this->input->post('title')),
				 /*获取栏目*/
				 'categoryes'    => $this->Categoryes_model->get_categoryes()
	          ); 
		/*将查询结果分配到视图中*/
	   $this->load->view('article_list',$data);

	}
	
	/*@创建文字列表并且加载栏目模型加载调用栏目
	*
	*
	*/
	 public function create()
	{
		/*从数据库中获取栏目，并将结果放入数组中*/
		$data=array(
	         'categoryes'=>$this->Categoryes_model->get_categoryes()
	          );
		/*分配变量，加载视图*/	  
		$this->load->view('article_create',$data);
	}
	/*添加文章*/
	public function add()
	{
		/*设置表单验证规则*/
     	$this->form_validation->set_rules('title', '文章标题', 'trim|required',
	    array(
			'required'  => '必须填写文章标题!'
		  )
		);
        $this->form_validation->set_rules('contents', '文章内容', 'required',	
		array(
			'required'  => '必须填写文文章内容!'
		  )
		  );
	
		/*判断客户端验证是否成功*/
   	    if ($this->form_validation->run() == FALSE){
			/*验证失败，返回错误信息，重新显示表单*/
			 $this->formTips(validation_errors(),"article/create",'2');
		} else {

		/*判断是否选中复选框*/
		if($this->input->post('auto')){
			/*如果选中复选框，则获取内容的前200字作为描述内容*/
			$description = strip_tags(character_limiter($this->input->post('contents'),$this->input->post('auto')));
		} else{
			/*如果没有选复选框，则获取输入的描述内容保存至变量中*/
			$description=$this->input->post('description');
		}

		/*将处理好的信息放入data数组*/
		$data = array(
		    'cid'         => $this->input->post('cid'),
			'title'       => $this->input->post('title'),
			'description' => $description,
			'contents'    => $this->input->post('contents'),
			'create_date' => date('Y-m-d H:i:s')
		);
		/*调用模型将数据插入数据库*/
	    $query = $this->Article_model->insert($data);
		/*判断返回结果，成功则作出提示*/
        if($query){
			/*调用父类的方法显示跳转页面*/
			 $this->formTips('添加成功',"article/index",'2');  
		  }
		  
		}
	}
	
	/*删除文章*/
	public function delete()
	{
		/*调用模型中的方法，删除文章*/
		$query = $this->Article_model->delete($this->uri->segment(3));
		/*根据返回结果判断操作情况，并重定向到原来的页面*/
		if($query){
			 $this->formTips('删除成功!',"article/index",'2');
		}
		 
	}
	
	/*批量删除和移动*/
	public function batch_operation(){
		
		/*将传入的id和cid保存至数组中*/
		$cid=$this->input->post('cid');
		$data=$this->input->post('id');
		
		/*批量删除*/
		if($this->input->post('submit')){
			/*如果删除的id为空，显示提示信息并重定向到原页面*/
			if(!$data){
		           $this->formTips('没有选择删除项!',"article/index",'2');
	        } 
			/*如果删除的id不空，则调用模型的方法批量删除文章*/
			else {
		  	    $query = $this->Article_model->deletec($data);
				/*根据返回的结果做出提示，并冲定向到原有页面*/
		        if($query){
			          $this->formTips('删除成功!',"article/index",'2');
		             }  
	             }
		}
		/*批量移动*/
		if($this->input->post('submit_move')){
			/*如果移动的文章和栏目id为空，显示提示信息并冲定向到原页面*/
			if(!$cid || !$data){
				$this->formTips('没有选择移动项!',"article/index",'2');
			}
			/*如果移动的文章和栏目不为空，则调用模型的方法批量移动文章*/
			else{
				$query = $this->Article_model->movec($cid,$data);
				/*根据返回的结果做出提示，并重定向到原有页面*/
				if($query){
					$this->formTips('移动成功!',"article/index",'2');
				 }  
			}	
		}

	}
	//图片上传
	

    /*编辑文章*/
	public function edit()
		{
			
		 $data=array(
			 /*根据传入的参数获取文章*/
	         'article'=>$this->Article_model->get_article($this->uri->segment(3)),
			 /*查询所有栏目*/
			 'categoryes'=>$this->Categoryes_model->get_categoryes()
	      );
		 /*加载视图，分配变量*/ 
		 $this->load->view('article_edit',$data);
		}
	
	/*修改文章*/
	public function update(){
        /*设置表单的客户端验证规则*/
     	$this->form_validation->set_rules('title', '文章标题', 'trim|required',
	    array(
			'required'  => '必须填写文章标题!'
		  )
		);
        $this->form_validation->set_rules('contents', '文章内容', 'required',	
		array(
			'required'  => '必须填写文文章内容!'
		  )
		  );
		
		/*如果客户端验证失败则输出错误提示，并重新加载修改文章内容页面*/
	    if ($this->form_validation->run() == FALSE){
			 $this->formTips(validation_errors(),"article/edit/".$this->uri->segment(3),'2');
		} else {
		/*如果自动设置描述内容被设置，则从内容中截取一段作为描述内容*/	
		if($this->input->post('auto')){
			$description = strip_tags(character_limiter($this->input->post('contents'),$this->input->post('auto')));
		} 
		/*如果自动设置描述内容未被设置，则根据手动输入的内容显示*/
		else{
		  $description=$this->input->post('description');
		}
		/*将新修改的文章内容等新信息放入数组内*/
		$data = array(
		    'cid'         => $this->input->post('cid'),
			'title'       => $this->input->post('title'),
			'contents'    => $this->input->post('contents'),
			'description' => $description
		);
		/*利用模型中的方法将原文章更新*/ 
	    $query = $this->Article_model->update($data,$this->uri->segment(3));
		/*根据返回的影响行数提示修改情况，并重定向到原页面*/
        if($query){
			 $this->formTips('修改成功',"article/index",'2');  
		  } else {
			 $this->formTips('没有任何更改',"article/index",'2');  
		  }
		  
		  }
	}
}
